<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

use app\library\Flag;

class MsgUser extends Model
{
    protected $table = 'msg_user';

    protected $pk = 'id';

    /**
     * 发消息
     *
     */
    public function add($arrInput)
    {
        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    /**
     * 删除 | 批量删除
     *
     */
    public function del($id)
    {

        $conds = [];

        if (is_array($id)) {
            $conds['id'] = ['in', $id];
        } else {
            $conds['id'] = ['=', $id];
        }

        $data = [
            'is_delete' => 1,
            'update_time' => time()
        ];

        return Db::table($this->table)->where($conds)->update($data);
    }

    /**
     * 清空消息
     *
     */
    public function clear($userid, $recv_user_id)
    {

        $conds = [
            'user_id' => ['=', $userid],
            'recv_user_id' => ['=', $recv_user_id],
            'is_delete' => ['=', 0],
        ];

        $data = [
            'is_delete' => 1,
            'update_time' => time()
        ];

        return Db::table($this->table)->where($conds)->update($data);
    }

    /**
     * 标记已读（批量）
     *
     */
    public function read($id)
    {

        if (is_array($id)) {
            $conds['id'] = ['in', $id];
        } else {
            $conds['id'] = ['=', $id];
        }

        $data = [
            'status' => 1,
            'update_time' => time()
        ];

        return Db::table($this->table)->where($conds)->update($data);
    }

    /**
     * 获取未读消息数
     *
     */
    public function getMsgUnReadCountByUserid($recv_user_id, $userid)
    {
        $arrCond = [
            'user_id' => ['=', $userid],
            'recv_user_id' => ['=', $recv_user_id],
            'status' => ['=', 0],
            'is_delete' => ['=', 0],
        ];

        $ret = Db::table($this->table)->where($arrCond)->select()->count();
        if (empty($ret)) {
            return 0;
        }
        return $ret;
    }

    /**
     * 批量获取未读消息数
     *
     */
    public function getMsgUnReadCountBatch($recv_user_id, $userids)
    {
        $arrCond = [
            'user_id' => ['in', $userids],
            'recv_user_id' => ['=', $recv_user_id],
            'status' => ['=', 0],
            'is_delete' => ['=', 0],
        ];

        $ret = Db::table($this->table)->where($arrCond)->field('user_id,count(user_id) as cnt')->group('user_id')->select();
        if (empty($ret)) {
            return [];
        }
        return $ret;
    }

    /**
     * 获取最新一条消息
     *
     */
    public function getLastMsgBatch($recv_user_id, $userid)
    {
        $arrCond = [
            'user_id' => ['in', $userid],
            'recv_user_id' => ['=', $recv_user_id],
            'is_delete' => ['=', 0],
        ];

        $ret = Db::table($this->table)->where($arrCond)->order('create_time desc')->group('user_id')->select();
        if (empty($ret)) {
            return [];
        }
        return $ret;
    }

    /**
     * 获取消息内容
     *
     */
    public function getMsg($recv_user_id, $userid, $fields = [], $offset = 0, $limit = 10)
    {
        $this->allowField(true);

        $arrCond = [
            'user_id' => ['in', [$userid, $recv_user_id]],
            'recv_user_id' => ['in', [$userid, $recv_user_id]],
            'is_delete' => ['=', 0],
        ];

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        $ret = Db::table($this->table)->where($arrCond)->field($arrField)->limit($offset, $limit)->select();

        if (empty($ret)) {
            return [];
        } else {
            return $ret;
        }
    }

}
